package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbSeckillGoodsMapper;
import com.dongdongshop.mapper.TbSeckillOrderMapper;
import com.dongdongshop.pojo.TbSeckillGoods;
import com.dongdongshop.pojo.TbSeckillGoodsExample;
import com.dongdongshop.pojo.TbSeckillOrder;
import com.dongdongshop.service.TbSeckillGoodService;
import com.dongdongshop.util.IdWorker;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


//秒杀商品表
@Service
@DubboService
public class TbSeckillGoodServiceimpl  implements TbSeckillGoodService {

    @Autowired
    private TbSeckillGoodsMapper tgm;//秒杀商品表


    @Autowired
    private TbSeckillOrderMapper tom;//秒杀订单表

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RocketMQTemplate rocketMQTemplate;





    //查询全部放入缓存
    @Override
    public void addall() {
        TbSeckillGoodsExample te=new TbSeckillGoodsExample();
        TbSeckillGoodsExample.Criteria criteria = te.createCriteria();
        criteria.andStatusEqualTo("1");
        Date now =new Date();
        criteria.andStartTimeLessThanOrEqualTo(now);//开始时间小于当前时间
        criteria.andEndTimeGreaterThanOrEqualTo(now);//结束时间大于当前时间
        List<TbSeckillGoods> tglist = tgm.selectByExample(te);
        if(tglist!=null){
            for (TbSeckillGoods tsgoods:tglist) {
                redisTemplate.boundHashOps("seckillGoods").put(tsgoods.getId(),tsgoods);
                //同步库存信息，把预减得库存放入缓存
                stringRedisTemplate.boundValueOps("stock_count"+tsgoods.getId()).set(tsgoods.getStockCount().toString());
            }
        }
    }


    //取出缓存数据
    @Override
    public List<TbSeckillGoods> getall() {
        List<TbSeckillGoods> seckillGoods = redisTemplate.boundHashOps("seckillGoods").values();
        return seckillGoods;
    }


    //查询一条数据根据id
    @Override
    public TbSeckillGoods getorder(Long id) {
        TbSeckillGoods goods = (TbSeckillGoods) redisTemplate.boundHashOps("seckillGoods").get(id);
        return goods;
    }


    //创建订单
    @Override
    public long createorder(Long sekillid, String username) {
        //先根据商品id从缓存中取出库存  s
        String s = stringRedisTemplate.boundValueOps("stock_count" + sekillid).get();
        Long aLong = Long.valueOf(s);
        if(aLong==0){//如果=0没货了
            return -1;
        }

        //如果不等于0每次走到这都要减一个库存
        Long decrement = stringRedisTemplate.boundValueOps("stock_count" + sekillid).decrement();//自减1

        //如果只剩一件，则需要将缓存删掉，库存同步数据库
        if(decrement==0){
            redisTemplate.boundHashOps("seckillGoods").delete(sekillid);
            TbSeckillGoods goods = (TbSeckillGoods) redisTemplate.boundHashOps("seckillGoods").get(sekillid);
            goods.setStockCount(0);
            tgm.updateByPrimaryKey(goods);
        }


        //异步生成订单，发送消息队列
        JSONObject json=new JSONObject();
        json.put("username",username);
        json.put("sekillid",sekillid);//转成json
        rocketMQTemplate.convertAndSend("seckill_topic",json.toString());
        System.out.println("发送成功");
        return 1;
    }




}
